<template>
    <div>
        <van-field label='标题' v-model="form.title"/>
        <van-field label='介绍' v-model="form.descrip"/>
        <van-field label='图片' v-model="form.pic"/>
        
        <van-field name="radio" label="单选框">
  <template #input>
    <van-radio-group v-model="form.status" direction="horizontal">
      <van-radio name="1">免费</van-radio>
      <van-radio name="2">收费</van-radio>
    </van-radio-group>
  </template>

  

</van-field>
        讲师：<select v-model="form.teacher_id">
            <option :value="i.id" v-for="i in teacher">{{i.name}}</option>
        </select><br>
        选择分类：<reply :data='cates'/>
        动态参数：
        <div v-if="params">
            <van-field v-for="(k,v) in params" :label='k' v-model="values[v]"/>
        </div>
        <van-button @click="add" color="linear-gradient(to right, #ff6034,#7232dd)">添加</van-button>
    </div>
</template>

<script>
import reply from '@/components/p7/reply'
import bus from '@/components/p7/bus.js'
    export default {
        components:{
            'reply':reply
        },
        data(){
            return{
                form:{pid:0,params:'',status:'2'},
                teacher:[{'id':1,'name':'张老师'},{'id':2,'name':'邢老师'},{'id':3,'name':'王老师'},{'id':4,'name':'焦老师'}],
                radio:'2',
                cates:{},
                params:{},
                values:{}
            }
        },
        methods:{
            add(){
                if(this.values){
                    this.form.params = JSON.stringify(this.values)
                }
                this.$axios.post('courseM',this.form).then(res=>{
                    alert('添加成功')
                })
            },
            get_list(){
                this.$axios.get('cateM').then(res=>{
                    this.cates = {'id':0,'name':'111','son':res.data.list}
                })
            }
        },
        mounted(){
            this.get_list()
            bus.$on('pid',val=>{
                this.form.pid = val
                console.log(val)
                // 根据pid查询分类，分类中的参数
                this.$axios.get('getCateByid?id='+val).then(res=>{
                    if(res.data.params){
                        this.params = JSON.parse(res.data.params)
                    }else{
                        this.params={}
                    }
                    
                })
            })
        }
    }
</script>

<style scoped>

</style>